---------------------------------------------------------------------------------------
-- RTBB PWM Init Script 
---------------------------------------------------------------------------------------
-- This file must be loaded after the init_base.lua ;
---------------------------------------------------------------------------------------

function PWMGetPWMPortCount()
	local nResult  = RTBB_PWMGetPWMPortCount(g_hBridgeBoard);
	dbgprintf ("PWM Port Count =%d\n",nResult );
	return nResult ;
end

function PWMStart(nPort)
	local nResult = RTBB_PWMStart( g_hBridgeBoard,nPort );
	if (resultMsg("RTBB_PWMStart",nResult) ~= true) 
	then
		return false;
	end
	
	return true;
end

function PWMStop(nPort)
	local nResult = RTBB_PWMStop( g_hBridgeBoard,nPort );
	if (resultMsg("RTBB_PWMStop",nResult) ~= true) 
	then
		return false;
	end
	
	return true;
end

function PWMGetStatus(nPort)
	ret_pwmGetStatus = RTBB_PWMGetStatus( g_hBridgeBoard,nPort );
	return ret_pwmGetStatus;
end

function PWMSetDutyCycle(nPort,fDutyCycle)
	local nResult = RTBB_PWMSetDutyCycle( g_hBridgeBoard,nPort ,fDutyCycle);
	if (resultMsg("RTBB_PWMSetDutyCycle",nResult) ~= true)
	then
		return false;
	end
	dbgprintf( "Set PWM Port%d Duty Cycle:%d\n", nPort, fDutyCycle )
	return true
end

function PWMGetDutyCycle(nPort)
	local nResult, fDutyCycle= RTBB_PWMGetDutyCycle( g_hBridgeBoard,nPort);
	if (resultMsg("RTBB_PWMGetDutyCycle",nResult)~= true)
	then
		return 0,false;
	end

	dbgprintf( "Get PWM Port%d Duty Cycle: %d\n", nPort, fDutyCycle );
	return fDutyCycle, true;
end

function PWMGetBaseClock(nPort)
	local nResult, pnClock= RTBB_PWMGetBaseClock( g_hBridgeBoard,nPort);
	if (resultMsg("RTBB_PWMGetBaseClock",nResult)~= true)
	then
		return 0,false;
	end

	dbgprintf( "Get PWM Port%d Base Clock: %d\n", nPort, pnClock );
	return pnClock, true;	
end

function PWMSetPeriod(nPort,nTick)	
	local nResult = RTBB_PWMSetPeriod( g_hBridgeBoard,nPort ,nTick);
	if (resultMsg("RTBB_PWMSetPeriod",nResult) ~= true)
	then
		return false;
	end
	dbgprintf( "Set PWM Port%d Period:%d\n", nPort, nTick )
	return true
end

function PWMSetOnPeriod(nPort,nOnTick)
	
	local nResult = RTBB_PWMSetOnPeriod( g_hBridgeBoard,nPort ,nOnTick);
	if (resultMsg("RTBB_PWMSetOnPeriod",nResult) ~= true)
	then
		return false;
	end
	dbgprintf( "Set PWM Port%d OnPeriod:%d\n", nPort, nOnTick )
	return true

end

function PWMGetPeriod(nPort)
	local nResult, pnTick= RTBB_PWMGetPeriod( g_hBridgeBoard,nPort);
	if (resultMsg("RTBB_PWMGetPeriod",nResult)~= true)
	then
		return 0,false;
	end

	dbgprintf( "Get PWM Port%d Period: %d\n", nPort, pnTick );
	return pnTick, true;
end

function PWMGetOnPeriod(nPort)
	local nResult, pnOnTick= RTBB_PWMGetOnPeriod( g_hBridgeBoard,nPort);
	if (resultMsg("RTBB_PWMGetOnPeriod",nResult)~= true)
	then
		return 0,false;
	end

	dbgprintf( "Get PWM Port%d OnPeriod: %d\n", nPort, pnOnTick );
	return pnOnTick, true;

end

function PWMSetPeriodByUs(nPort,nUs)
	local nResult = RTBB_PWMSetPeriodByUs( g_hBridgeBoard,nPort ,nUs);
	if (resultMsg("RTBB_PWMSetPeriodByUs",nResult) ~= true)
	then
		return false;
	end
	dbgprintf( "Set PWM Port%d Period By Us:%d\n", nPort, nUs )
	return true
end

function PWMSetOnPeriodByUs(nPort,nUs)
	local nResult = RTBB_PWMSetOnPeriodByUs( g_hBridgeBoard,nPort ,nUs);
	if (resultMsg("RTBB_PWMSetOnPeriodByUs",nResult) ~= true)
	then
		return false;
	end
	dbgprintf( "Set PWM Port%d OnPeriod By Us:%d\n", nPort, nUs )
	return true
end


function PWMSetPeriodByMs(nPort,nMs)
	local nResult = RTBB_PWMSetPeriodByMs( g_hBridgeBoard,nPort,nMs);
	if (resultMsg("RTBB_PWMSetPeriodByMs",nResult) ~= true)
	then
		return false;
	end
	dbgprintf( "Set PWM Port%d Period By Ms:%d\n", nPort, nMs )
	return true
end

function PWMSetOnPeriodByMs(nPort,nMs)
	local nResult = RTBB_PWMSetOnPeriodByMs( g_hBridgeBoard,nPort,nMs);
	if (resultMsg("RTBB_PWMSetOnPeriodByMs",nResult) ~= true)
	then
		return false;
	end
	dbgprintf( "Set PWM Port%d OnPeriod By Ms:%d\n", nPort, nMs )
	return true
end



function PWMGetPeriodByUs(nPort)
	local nResult, pnUs= RTBB_PWMGetPeriodByUs( g_hBridgeBoard,nPort);
	if (resultMsg("RTBB_PWMGetPeriodByUs",nResult)~= true)
	then
		return 0,false;
	end

	dbgprintf( "Get PWM Port%d Period By Us: %d\n", nPort, pnUs );
	return pnUs, true;

end

function PWMGetOnPeriodByUs(nPort)
	local nResult, pnUs= RTBB_PWMGetOnPeriodByUs( g_hBridgeBoard,nPort);
	if (resultMsg("RTBB_PWMGetOnPeriodByUs",nResult)~= true)
	then
		return 0,false;
	end

	dbgprintf( "Get PWM Port%d OnPeriod By Us: %d\n", nPort, pnUs );
	return pnUs, true;
end